package com.idroc.storage.support;
 
import com.idroc.storage.IoBlock;
import com.idroc.storage.IoBlockFile;

/**
 * 复合IO块文件
 * @author yuyoo (yuyoo4j@163.com)
 * @teme 2010-4-10 下午11:17:01
 */
public class ComposeLocalIoBlockFile implements IoBlockFile {
	
	/**
	 * 分配每个内部IO块的IO块文件
	 */
	private IoBlockFile itemFile = null;
	
	/**
	 * 内部IO块个数
	 */
	private short itemCount = 0;
	
	/**
	 * 复合IO块大小
	 */
	private int blockSize = 0;
	
	/**
	 * 构造复合IO块文件
	 * @param itemFile -- 内部IO块的IO块文件
	 * @param itemCount -- 内部IO块个数
	 */
	ComposeLocalIoBlockFile(IoBlockFile itemFile, short itemCount) {
		
		this.itemFile = itemFile;
		this.itemCount = itemCount;
		this.blockSize = itemFile.getBlockSize() * itemCount;
	}

	public int getBlockSize() {
		
		return blockSize;
	}

	public int getId() {

		return itemFile.getId();
	}

	public String getName() {

		return itemFile.getName();
	}

	public IoBlock newIoBlock(long offset, byte[] backend, int backendOffset) {

		IoBlock item = itemFile.newIoBlock(offset, backend, backendOffset);
		ComposeFileIoBlock newOne = new ComposeFileIoBlock(item, itemCount, backend, backendOffset);
		return newOne;
	} 

	public IoBlock read(long offset) {

		byte[] backend = new byte[blockSize];
		IoBlock item = itemFile.newIoBlock(offset, backend, 0);
		ComposeFileIoBlock newOne = new ComposeFileIoBlock(item, itemCount, backend, 0);
		newOne.load();
		return newOne;
	}

	public boolean readData(long offset, byte[] data, int start, int length) {
		
		return itemFile.readData(offset, data, start, length);
	}

	public boolean write(IoBlock block) {

		return itemFile.write(block);
	}

	public boolean writeData(long offset, byte[] data, int start, int length) {

		return itemFile.writeData(offset, data, start, length);
	}

}
